package it.unisa.services;

import java.util.List;

import it.unisa.beans.Contour;
import it.unisa.dao.ContourDao;
import it.unisa.dao.PetroglyphDao;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * TODO remove contour
 * 
 * @author Alfonso
 *
 */
public class ContourWS {
	public boolean addContour(Contour c) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		ContourDao cont=(ContourDao)appContext.getBean("contourDao");
		cont.insert(c);
		return true;
	}
	
	public it.unisa.beanService.Contour getContourByURL(String url) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		ContourDao cont=(ContourDao)appContext.getBean("contourDao");
		List<Contour> lista = cont.findAllContours();
		for(int i=0; i<lista.size(); i++) {
			if(lista.get(i).getUrl_contour().equals(url)) {
				Contour temp = lista.get(i);
				it.unisa.beanService.Contour ret = new it.unisa.beanService.Contour();
				ret.setDate(temp.getDate().toString());
				ret.setHour(temp.getHour().toString());
				ret.setId_account(temp.getAccount().getSsn());
				ret.setUrl_contour(url);
				ret.setId_photo(temp.getPhoto().getId());
				return ret;
			}
		}
		return null;
	}
	
	public boolean removeContour(Contour contour){
		return true;
	}
	
	public it.unisa.beanService.Contour[] getAllCountour() {
		ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
		ContourDao cont=(ContourDao)appContext.getBean("contourDao");
		List<Contour> lista = cont.findAllContours();
		it.unisa.beanService.Contour[] retArr = new it.unisa.beanService.Contour[lista.size()];
		for(int i=0; i<lista.size(); i++) {
			Contour temp = lista.get(i);
			it.unisa.beanService.Contour ret = new it.unisa.beanService.Contour();
			ret.setDate(temp.getDate().toString());
			ret.setHour(temp.getHour().toString());
			ret.setId_account(temp.getAccount().getSsn());
			ret.setUrl_contour(temp.getUrl_contour());
			ret.setId_photo(temp.getPhoto().getId());
			retArr[i] = ret;
		}
		return retArr;
	}
}
